#include <xy/render/context.h>

namespace xy
{

namespace render
{

using namespace xy::geom;
using namespace xy::utils;

context::context(int width, int height, int msaaLevel)
    : m_painter(std::make_shared<render::painter>(width, height, msaaLevel)),
      m_camera(std::make_shared<geom::camera>(10.0f, 90.0f, 0.0f, 30.0f, (float)width / height))
{
}

auto context::painter() const -> decltype(m_painter)
{
    return m_painter;
}

auto context::camera() const -> decltype(m_camera)
{
    return m_camera;
}

image_t context::texture(std::string name) const
{
    return m_textures.at(name);
}

void context::create_texture(std::string filename)
{
    image_t image;
    image.read(filename.c_str());
    m_textures[filename] = image;
}

void context::create_texture(Texture texture)
{
    std::uint8_t rgba[] = {0, 0, 0, 255};

    switch (texture)
    {
    case Yellow2d:
        rgba[0] = rgba[1] = 255;
        break;
    case Red2d:
        rgba[0] = 255;
        break;
    case Green2d:
        rgba[1] = 255;
        break;
    case Blue2d:
        rgba[2] = 255;
        break;
    case White2d:
        rgba[0] = rgba[1] = rgba[2] = 255;
        break;
    case Black2d:
    case Chess:
        break;
    default:
        std::cerr << "Texture don't exists." << std::endl;
        break;
    }

    image_t img(16, 16, 4);
    for (int i = 0; i < 16; i++)
    {
        for (int j = 0; j < 16; j++)
        {
            if (texture == Chess && (i + j) % 2 == 0)
                img.set(i, j, 0xffffffff);
            else
                img.set(i, j, color_t(rgba, 4));
        }
    }

    m_textures[enum_name(texture)] = img;
}

} // namespace render

} // namespace xy